package com.sicnu.startup.controller;


import com.sicnu.startup.entity.Users;
import com.sicnu.startup.exception.StartupException;
import com.sicnu.startup.result.Result;
import com.sicnu.startup.result.ResultCodeEnum;
import com.sicnu.startup.service.UsersService;
import com.sicnu.startup.vo.LoginVO;
import com.sicnu.startup.vo.RegisterVO;
import com.sicnu.startup.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zr
 * @since 2022-01-06
 */
@RestController
@RequestMapping("/startup/users")
@CrossOrigin
public class UsersController {
    @Resource
    private UsersService usersService;

    @PostMapping("register")
    public Result registerUser(@RequestBody RegisterVO registerVO,HttpServletRequest request) {
        Boolean flag = usersService.registerUser(registerVO,request);
        return flag ? Result.ok().message("注册成功") : Result.fail().message("注册失败");
    }

    @PostMapping("login")
    public Result loginUser(@RequestBody LoginVO loginVO,HttpServletRequest request) {
        Users users = usersService.login(loginVO,request);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(users, userVO);
        return Result.ok(userVO).message("登录成功");
    }

}

